<div class="form-group <?=$widget_data['name']?>_field">
    <label class="<?=(isset($widget_data['sm']) && $widget_data['sm']=='2')?'col-sm-2':'col-sm-3'?> control-label <?=(isset($widget_data['required']))?'is-required':''?>"><?=$widget_data['title']?$widget_data['title'].'：':''?></label>
    <div class="<?=(isset($widget_data['sm']) && $widget_data['sm']=='2')?'col-sm-9':'col-sm-8'?>">
        <div class="b5uploadmainbox">
            <a href="javascript:;" class="btn btn-primary btn-sm"
                <?= $widget_data['id']?'id="'.$widget_data['id'].'"':''?>
               data-multi="<?=$widget_data['multi']??'false'?>"
               data-name="<?=$widget_data['name']??'uploadimg'?>"
               data-cat="<?=$widget_data['cat']??'images'?>"
               data-width="<?=$widget_data['width']??'0'?>"
               data-height="<?=$widget_data['height']??'0'?>"
               data-water="<?=$widget_data['water']??''?>"
               data-exts="<?=$widget_data['exts']??''?>"
               data-is_oss="<?=$widget_data['is_oss']??''?>"
            ><i class="fa fa-image"></i>上传文件</a>
            <?php if(!isset($widget_data['link']) || $widget_data['link']!='false'):?>
                或
                <div class="uploadimg_link">
                    <input type="text" class="form-control" id="<?=$widget_data['id']?>_link"><a href="javascript:;" class="btn btn-primary btn-sm" id="<?=$widget_data['id']?>_linkbtn"><i class="fa fa-link"></i>添加</a>
                </div>
            <?php endif;?>
            <?php if(isset($widget_data['tips'])):?>
                <span class="help-block m-b-none"><?php if($widget_data['tips']):?><i class="fa fa-info-circle"></i> <?=$widget_data['tips']?> <?php endif;?></span>
            <?php endif;?>
            <div class="b5uploadlistbox <?=$widget_data['name']?>_imglist" id="<?=$widget_data['name']?>_imglist">
                <?php if(isset($widget_data['data']) && $widget_data['data']):?>
                    <?php foreach(is_array($widget_data['data'])?$widget_data['data']:explode(',',$widget_data['data']) as $imglink):?>
                        <div class="b5uploadimg_li">
                            <input type="hidden" name="<?=$widget_data['name']?>[]" value="<?=$imglink?>">
                            <div class="b5uploadimg_con">
                                <div class="b5uploadimg_cell">
                                    <img src="/public/static/admin/images/upload.png" alt="">
                                </div>
                            </div>
                            <div class="b5uploadimg_footer">
                                <a href="javascript:;" onclick="b5uploadfileRemove(this)"><i class="fa fa-trash-o"></i>删除</a>
                                <a href="<?=$imglink?>" target="_blank"><i class="fa fa-hand-o-right"></i>查看</a>
                            </div>
                        </div>
                    <?php endforeach;?>
                <?php elseif(isset($widget_data['info']) && isset($widget_data['info'][$widget_data['name']]) && $widget_data['info'][$widget_data['name']]):?>
                    <?php foreach(is_array($widget_data['info'][$widget_data['name']])?$widget_data['info'][$widget_data['name']]:explode(',',$widget_data['info'][$widget_data['name']]) as $imglink):?>
                        <div class="b5uploadimg_li">
                            <input type="hidden" name="<?=$widget_data['name']?>[]" value="<?=$imglink?>">
                            <div class="b5uploadimg_con">
                                <div class="b5uploadimg_cell">
                                    <img src="/public/static/admin/images/upload.png" alt="">
                                </div>
                            </div>
                            <div class="b5uploadimg_footer">
                                <a href="javascript:;" onclick="b5uploadfileRemove(this)"><i class="fa fa-trash-o"></i>删除</a>
                                <a href="<?=$imglink?>" target="_blank"><i class="fa fa-hand-o-right"></i>查看</a>
                            </div>
                        </div>
                    <?php endforeach;?>
                <?php endif;?>
            </div>
        </div>
    </div>
</div>
<?php $this->beginBlock('script_after'.($widget_data['script_after']??'')); ?>
<script>
    $(function () {
        b5uploadfileinit("<?=$widget_data['id']?>");
        <?php if(isset($widget_data['drag'])):?>
            dragula([<?=$widget_data['name']?>_imglist]);
        <?php endif;?>

        function b5uploadfileLink(id) {
            var obj=$("#"+id);
            var name=obj.data('name');
            $("#"+id+"_linkbtn").click(function () {
                var linkval=$("#"+id+"_link").val();
                if($.common.isEmpty(linkval)){
                    $.modal.msgWarning("请输入图片链接");
                }else{
                    var html=b5uploadfilehtml(linkval,name);
                    b5uploadfilehtmlshow(id,html);
                    $("#"+id+"_link").val('');
                }
            });
        }
        function b5uploadfilehtmlshow(id,html) {
            var obj=$("#"+id);
            var listbox=obj.parents(".b5uploadmainbox").find(".b5uploadlistbox");
            var maxnum=obj.data('multi');
            if($.common.isEmpty(maxnum) || !maxnum || maxnum==='false'){
                maxnum=1;
            }
            if(maxnum===true || maxnum==="true") maxnum=10;
            var multi = maxnum > 1 ? true : false;
            if(multi){
                listbox.append(html);
            }else{
                listbox.html(html);
            }
        }
//删除上传文件
        function b5uploadfileRemove(obj) {
            $(obj).parents(".b5uploadimg_li").remove();
        }
//上传文件按钮初始化
        function b5uploadfileinit(id,callback) {
            var obj=$("#"+id);
            var name=obj.data('name');
            var maxnum=obj.data('multi');
            if($.common.isEmpty(maxnum) || !maxnum || maxnum==='false'){
                maxnum=1;
            }
            if(maxnum===true || maxnum==="true") maxnum=10;
            var multi = maxnum > 1 ? true : false;

            b5uploadfileLink(id);
            layui.use('upload', function(){
                var upload = layui.upload;
                var loadingIndex;
                //执行实例
                var uploadInst = upload.render({
                    elem: '#'+id //绑定元素
                    ,url: upFileUrl //上传接口
                    ,field:'file'
                    ,multiple:multi
                    ,number:maxnum
                    ,size:1024*20
                    ,data:{
                        width:function(){
                            return $.common.isEmpty($("#"+id).attr('data-width'))?0:$("#"+id).attr('data-width');
                        },
                        height:function () {
                            return $.common.isEmpty($("#"+id).attr('data-height'))?0:$("#"+id).attr('data-height');
                        },
                        cat:function () {
                            return $.common.isEmpty($("#"+id).attr('data-cat'))?'':$("#"+id).attr('data-cat');
                        },
                        water:function () {
                            return $.common.isEmpty($("#"+id).attr('data-water'))?'':$("#"+id).attr('data-water');
                        },
                        exts:function () {
                            return $.common.isEmpty($("#"+id).attr('data-exts'))?'':$("#"+id).attr('data-exts');
                        },
                        is_oss:function () {
                            return $.common.isEmpty($("#"+id).attr('data-is_oss'))?'':$("#"+id).attr('data-is_oss');
                        },
                    }
                    ,accept:'file'
                    ,before:function () {
                        loadingIndex = layer.load(2, { shade: [0.5, '#ccc'] });
                    }
                    ,done: function(res){
                        layer.close(loadingIndex);
                        if(res.success && res.code===0){
                            if($.common.isFunction(callback)){
                                callback(id,res.data);
                            }else{
                                var html=b5uploadfilehtml(res.data.path,name,res.data.url);
                                b5uploadfilehtmlshow(id,html)
                            }
                        }else{
                            $.modal.msgError(res.msg)
                        }
                    }
                    ,error: function(){
                        $.modal.msgWarning('网络连接错误')
                    }
                });
            });
        }
//上传图片成功后的展示
        function b5uploadfilehtml(path,name,url=''){
            url = url?url:path
            var html='<div class="b5uploadimg_li">' +
                '           <input type="hidden" name="'+name+'[]" value="'+path+'">' +
                '           <div class="b5uploadimg_con">' +
                '                <div class="b5uploadimg_cell">' +
                '                     <img src="/public/static/admin/images/upload.png" alt="">' +
                '                </div>' +
                '            </div>' +
                '            <div class="b5uploadimg_footer">' +
                '                 <a href="javascript:;" onclick="b5uploadfileRemove(this)"><i class="fa fa-trash-o"></i>删除</a>' +
                '                  <a href="'+url+'" target="_blank"><i class="fa fa-hand-o-right"></i>查看</a>' +
                '            </div>' +
                '      </div>';
            return html;
        }
    })
</script>
<?php $this->endBlock(); ?>
